package com.qf.sy2103.fmmall.service.impl;

import com.qf.sy2103.common.vo.ResStatus;
import com.qf.sy2103.common.vo.ResultVO;
import com.qf.sy2103.fmmall.beans.entity.Category;
import com.qf.sy2103.fmmall.beans.vo.CategoryVo;
import com.qf.sy2103.fmmall.mapper.CategoryMapper;
import com.qf.sy2103.fmmall.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

@Service
public class CategoryServiceImpl implements CategoryService {

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    CategoryMapper categoryMapper;

//    @Autowired
//    RedisTemplate redisTemplate;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ResultVO categorylist() {

        final ResultVO resultVO = new ResultVO();

        // 从redis缓存中查询是否已经有分类数据的缓存了，如果有，则直接返回
        List<CategoryVo> voList = null;
//        voList= (List<CategoryVo>) redisTemplate.opsForValue().get("categorylist");
//        if(voList!=null){
//            resultVO.setData(voList);
//            resultVO.setCode(ResStatus.OK);
//            resultVO.setMsg("success");
//            return resultVO;
//        }

        // 去关系数据库中查询数据
        try {
            // 1. 查询商品的一级分类
            final Example example = new Example(Category.class);
            final Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("categoryLevel","1");
            final List<Category> categories1 = categoryMapper.selectByExample(example);
            List<CategoryVo> categoryVos1 = castToVoList(categories1);

            // 遍历查询到的所有一级分类，查询每个一级分类下属的二级分类
            for (CategoryVo categoryVo1 : categoryVos1) {

                final Example example1 = new Example(Category.class);
                final Example.Criteria criteria1 = example1.createCriteria();
                criteria1.andEqualTo("parentId",categoryVo1.getCategoryId());
                final List<Category> categories2 = categoryMapper.selectByExample(example1);
                final List<CategoryVo> categoryVos2 = castToVoList(categories2);

                // 遍历所有二级分类，查询二级分类下的三级分类
                for (CategoryVo categoryVo2 : categoryVos2) {
                    final Example example2 = new Example(Category.class);
                    final Example.Criteria criteria2 = example2.createCriteria();
                    criteria2.andEqualTo("parentId",categoryVo2.getCategoryId());
                    final List<Category> categories3 = categoryMapper.selectByExample(example2);
                    final List<CategoryVo> categoryVos3 = castToVoList(categories3);
                    categoryVo2.setCategories(categoryVos3);
                }

                categoryVo1.setCategories(categoryVos2);
            }

            // 过滤掉不包含任何二级分类的一级分类。
//            for (CategoryVo categoryVo : categoryVos1) {
//                if (categoryVo.getCategories().size()==0){
//                    categoryVos1.remove(categoryVo);
//                }
//            }
            // 利用java8的 stream 进行list的过滤
            voList = categoryVos1.stream().filter(categoryVo -> categoryVo.getCategories().size() != 0).collect(Collectors.toList());

            // 把查到的商品分类数据缓存到 redis中
//            redisTemplate.opsForValue().set("categorylist",voList);

            resultVO.setData(voList);
            resultVO.setCode(ResStatus.OK);
            resultVO.setMsg("success");
        }catch (Exception e){
            e.printStackTrace();
            resultVO.setCode(ResStatus.NO);
            resultVO.setMsg("failed");
        }

        return resultVO;
    }

    private List<CategoryVo> castToVoList(List<Category> categories1) {

        final ArrayList<CategoryVo> categoryVos = new ArrayList<>();
        for (Category category : categories1) {
            final CategoryVo categoryVo = new CategoryVo();
            BeanUtils.copyProperties(category,categoryVo);
            categoryVos.add(categoryVo);
        }

        return categoryVos;
    }


}
